//
//  NRLSVMAllScreenViewController.m
//  NaRiLSVMT
//
//  Created by zhangmeng on 14-8-25.
//  Copyright (c) 2014年 helloyouth.org. All rights reserved.
//

#import "NRLSVMAllScreenViewController.h"
#import "UIImageView+AFNetworking.h"
#import "Network_Operation.h"
#import "TcpClient.h"
#import "MBProgressController.h"



extern  BOOL    isFlowProjectPlay;//是哪个模块进行的双击大图操作(流程控制or屏幕预览)
extern  NSMutableArray  *nodeNameArrForProject;//用于投屏前双击，的标题字符串显示
extern  NSString    *currentNodeName;//用于投屏前双击，的标题字符串显示

@interface NRLSVMAllScreenViewController ()

@end

@implementation NRLSVMAllScreenViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIPanGestureRecognizer *pangesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.showView addGestureRecognizer:pangesture];
    
    [self counstAceView];
    CGFloat imWidth;
    if(![@"0" isEqualToString:StringFromId(self.arrData[self.clickNum][@"width"])] && ![@"0" isEqualToString:StringFromId(self.arrData[self.clickNum][@"height"])]){
        imWidth = (Screen_Width-28)/([self.arrData[self.clickNum][@"width"] intValue]/[self.arrData[self.clickNum][@"height"] intValue]);
    }
    self.imageView = [[UIImageView alloc] init];
    self.imageView.frame = Frame(14,150,(Screen_Width-28),imWidth);
    [self.showView addSubview:self.imageView];
    [self.imageView setImage:[[DownloadImage sharedInstance] getUrlImage:StringFromId(self.arrData[self.clickNum][@"templateId"]) Block:^(UIImage *image){
        [self.imageView setImage:image];
    }]];
    
    [[TimerDownImage sharedInstance] downCallBackData:StringFromId(self.arrData[self.clickNum][@"templateId"]) CallBack:^(UIImage *image){
        [self.imageView setImage:image];
    }];
    
    if(isFlowProjectPlay == NO)
    {
        NSString *name = [NSString stringWithFormat:@"%@ %@ %@   %ld%@%lu",self.screenNum,@">",StringFromId(self.arrData[self.clickNum][@"name"]),self.clickNum+1,@"/",(unsigned long)self.arrData.count];
        self.screenId.font = Font(18.0);
        self.screenId.text = name;
    }
    else
    {
        NSString    *tmpName = StringFromId(self.arrData[self.clickNum][@"name"]);
        NSInteger index = [tmpName rangeOfString:@"【"].location;
        if(![Common isEmptyString:tmpName])
        tmpName = [tmpName substringWithRange:NSMakeRange(0, index)];
        else
        tmpName = NullString;
        NSString    *tmpScreenNum = [NSString stringWithFormat:@"%@ %@ %@",self.screenNum,@">",currentNodeName];
        
        NSString *name = [NSString stringWithFormat:@"%@ %@ %@",tmpScreenNum,@">",tmpName];
        self.screenId.font = Font(18.0);
        self.screenId.text = name;
    }
    
    self.proView = [[PromtViewController alloc] init];
    self.proView.view.frame = Frame(205, 163, 617, 417);
    self.proView.screenId = self.forscreenId;
    self.proView.delegate = self;

    self.promeView = [[UIView alloc] init];
    self.promeView.frame = Frame(0,0,1024,768);
    self.promeView.backgroundColor = [UIColor blackColor];
    self.promeView.alpha = 0.3;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappro:)];
    [self.promeView addGestureRecognizer:tapGesture];
    self.talkbtnflag = YES;
    
    UIButton *btnBak = [UIButton buttonWithType:UIButtonTypeCustom];
    btnBak.frame = CGRectMake(965,715,40,40);
    [btnBak setBackgroundImage:[UIImage imageNamed:@"scan_lessen.png"] forState:UIControlStateNormal];
    [btnBak addTarget:self action:@selector(backBtnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnBak];
    // Do any additional setup after loading the view from its nib.
}
-(void)counstAceView{
    self.aceDrawView = [[ACEDrawingView alloc] init];
    CGFloat imWidth;
    if(![@"0" isEqualToString:StringFromId(self.arrData[self.clickNum][@"width"])] && ![@"0" isEqualToString:StringFromId(self.arrData[self.clickNum][@"height"])]){
         imWidth = (Screen_Width-28)/([self.arrData[self.clickNum][@"width"] intValue]/[self.arrData[self.clickNum][@"height"] intValue]);
    }
    
    self.aceDrawView.frame = CGRectMake(14,212,(Screen_Width-28),imWidth);
    self.aceDrawView.backgroundColor = [UIColor clearColor];
    [self.aceDrawView setLineColor:ClearColor];
    [self.aceDrawView setLineWidth:0.0f];
    if(![@"0" isEqualToString:StringFromId(self.arrData[self.clickNum][@"width"])] && ![@"0" isEqualToString:StringFromId(self.arrData[self.clickNum][@"height"])]){
    self.aceDrawView.zoomX = [self.arrData[self.clickNum][@"width"] intValue]/(Screen_Width-28);
    self.aceDrawView.zoomY = [self.arrData[self.clickNum][@"width"] intValue]/(Screen_Width-28);
    self.aceDrawView.screenWidth = [self.arrData[self.clickNum][@"width"] intValue];
        self.aceDrawView.screenHeight = [self.arrData[self.clickNum][@"height"] intValue];
    }
    self.aceDrawView.forScreenFlag = NO;
    self.aceDrawView.doubleClickflag = YES;
    self.aceDrawView.drawingMode = DrawingModeNoneing;
    self.aceDrawView.backgroundColor = [UIColor clearColor];
    [self.aceDrawView configure];
    
    [AddNoticeObserver(NoticeKey_EventNull){
        [self.aceDrawView removeFromSuperview];
        self.aceDrawView = nil;
        self.talkbtnflag = YES;
        [self.talkBtn setImage:[UIImage imageNamed:@"forscreen_talk.png"] forState:UIControlStateNormal];
    }];
}
-(void)viewDidUnload
{
    [super viewDidUnload];
    self.imageView = nil;
    
     self.aceDrawView = nil;    //交互的view
    self.proView = nil;   //提示的view
     self.arrData = nil; // 缩略图数据
     self.promeView = nil; //覆盖的view
     self.forscreenId = nil; //屏幕的Id
    self.screenNum = nil; // 屏幕的名称

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    self.imageView = nil;
    self.aceDrawView = nil;    //交互的view
    self.proView = nil;   //提示的view
    self.arrData = nil; // 缩略图数据
    self.promeView = nil; //覆盖的view
    self.forscreenId = nil; //屏幕的Id
    self.screenNum = nil; // 屏幕的名称
    // Dispose of any resources that can be recreated.
}
-(IBAction)backBtnclick:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)pan:(UIPanGestureRecognizer *)recognizer
{
    //begin pan...
    if(self.talkbtnflag){
     CGFloat initTouchPosX = 0;
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        initTouchPosX = [recognizer locationInView:self.showView].x;
    }
    else if (recognizer.state == UIGestureRecognizerStateChanged) { //status change
        CGFloat panAmount = initTouchPosX - [recognizer locationInView:self.showView].x;
        // if (self.isExpand) {
        if (panAmount < 0) {
            //右移
            self.direction = PanDirectionRight;
        }
        else {
            if (panAmount > 0) {
                //左移
                self.direction = PanDirectionLeft;
            }
        }
    }
    else if (recognizer.state == UIGestureRecognizerStateEnded ||
             recognizer.state == UIGestureRecognizerStateCancelled){
        [self slideOutContentViewInDirection:self.direction];
    }
    }
}
#pragma -mark
#pragma -mark Custom Methods
- (void)slideOutContentViewInDirection:(PanDirection)direction {
    switch (direction) {
        case PanDirectionRight:{
            self.clickNum--;
            if(self.clickNum == -1)
                self.clickNum = self.arrData.count-1;

            [self.imageView setImage:[[DownloadImage sharedInstance] getUrlImage:StringFromId
                                      (self.arrData[self.clickNum][@"templateId"]) Block:^(UIImage *image){
                [self.imageView setImage:image];
            }]];
            
            [[TimerDownImage sharedInstance] downCallBackData:StringFromId(self.arrData[self.clickNum][@"templateId"]) CallBack:^(UIImage *image){
                [self.imageView setImage:image];
            }];
            if ([self.arrData[self.clickNum][@"height"] intValue] == 0 || [self.arrData[self.clickNum][@"width"] intValue] == 0)
            {
                return;
            }
            CGFloat imWidth;
            if(![@"0" isEqualToString:StringFromId(self.arrData[self.clickNum][@"width"])] && ![@"0" isEqualToString:StringFromId(self.arrData[self.clickNum][@"height"])]){
           imWidth = (Screen_Width-28)/([self.arrData[self.clickNum][@"width"] intValue]/[self.arrData[self.clickNum][@"height"] intValue]);
            }
            CGRect r = self.imageView.frame;
            r.size.height = imWidth;
            self.imageView.frame = r;
            if(![@"0" isEqualToString:StringFromId(self.arrData[self.clickNum][@"width"])] && ![@"0" isEqualToString:StringFromId(self.arrData[self.clickNum][@"height"])]){
            self.aceDrawView.zoomX = [self.arrData[self.clickNum][@"width"] intValue]/(Screen_Width-28);
            self.aceDrawView.zoomY = [self.arrData[self.clickNum][@"width"] intValue]/(Screen_Width-28);
            }
            if(isFlowProjectPlay == NO)
            {
                NSString *name = [NSString stringWithFormat:@"%@ %@ %@   %ld%@%lu",self.screenNum,@">",StringFromId(self.arrData[self.clickNum][@"name"]),self.clickNum+1,@"/",(unsigned long)self.arrData.count];
                self.screenId.text = name;
            }
            else
            {
                NSString    *tmpName = StringFromId(self.arrData[self.clickNum][@"name"]);
                NSInteger index = [tmpName rangeOfString:@"【"].location;
                tmpName = [tmpName substringWithRange:NSMakeRange(0, index)];
                
                NSString    *tmpScreenNum = [NSString stringWithFormat:@"%@ %@ %@",self.screenNum,@">",nodeNameArrForProject[self.clickNum]];
                
                NSString *name = [NSString stringWithFormat:@"%@ %@ %@",tmpScreenNum,@">",tmpName];
                self.screenId.text = name;
            }
            
            }
            break;
        case PanDirectionLeft:{
           self.clickNum++;
            if(self.clickNum == (self.arrData.count))
                self.clickNum = 0;
            
            
            [self.imageView setImage:[[DownloadImage sharedInstance] getUrlImage:StringFromId
                                      (self.arrData[self.clickNum][@"templateId"]) Block:^(UIImage *image){
                                          [self.imageView setImage:image];
                                      }]];
            [[TimerDownImage sharedInstance] downCallBackData:StringFromId(self.arrData[self.clickNum][@"templateId"]) CallBack:^(UIImage *image){
                [self.imageView setImage:image];
            }];
            
            if ([self.arrData[self.clickNum][@"height"] intValue] == 0 || [self.arrData[self.clickNum][@"width"] intValue] == 0)
            {
                return;
            }
            
            CGFloat imWidth = (Screen_Width-28)/([self.arrData[self.clickNum][@"width"] intValue]/[self.arrData[self.clickNum][@"height"] intValue]);
            CGRect r = self.imageView.frame;
            r.size.height = imWidth;
            self.imageView.frame = r;
            self.aceDrawView.zoomX = [self.arrData[self.clickNum][@"width"] intValue]/(Screen_Width-28);
            self.aceDrawView.zoomY = [self.arrData[self.clickNum][@"width"] intValue]/(Screen_Width-28);
            
            if(isFlowProjectPlay == NO)
            {
                NSString *name = [NSString stringWithFormat:@"%@ %@ %@   %ld%@%lu",self.screenNum,@">",StringFromId(self.arrData[self.clickNum][@"name"]),self.clickNum+1,@"/",(unsigned long)self.arrData.count];
                self.screenId.text = name;
            }
            else
            {
                NSString    *tmpName = StringFromId(self.arrData[self.clickNum][@"name"]);
                if (tmpName == nil)
                {
                    tmpName = @"";
                }
                else
                {
                    NSInteger index = [tmpName rangeOfString:@"【"].location;
                    if (index == NSNotFound) {
                        tmpName = @"";
                    }
                    else
                    {
                        tmpName = [tmpName substringWithRange:NSMakeRange(0, index)];
                    }
                }
                
                NSString    *tmpScreenNum = (self.screenNum == nil || nodeNameArrForProject[self.clickNum] == nil)?@"":[NSString stringWithFormat:@"%@ %@ %@",self.screenNum,@">",nodeNameArrForProject[self.clickNum]];
                NSString *name = [NSString stringWithFormat:@"%@ %@ %@",tmpScreenNum,@">",tmpName];
                self.screenId.text = name;
            }
            
    }
            break;
        default:
            break;
    }

}
-(IBAction)messageBtnclick:(id)sender
{
    //弹出提示框
    [self.view addSubview:self.promeView];
    [self.view addSubview:self.proView.view];
    [self.messageBtn setImage:[UIImage imageNamed:@"forscreen_messageclick.png"] forState:UIControlStateNormal];
    //流程控制 屏幕预览的区别
    if(isFlowProjectPlay == NO){
    [self.proView showViewblock:StringFromId(self.arrData[self.clickNum][@"templateId"]) Name:self.arrData[self.clickNum][@"name"]];
    }
    else
    {
        NSString    *strName = self.arrData[self.clickNum][@"name"];
        if (!([strName rangeOfString:@":"].location == NSNotFound))
        {
            NSInteger   index = [strName rangeOfString:@":"].location;
            strName = [strName substringWithRange:NSMakeRange(index+1, strName.length-index-1-1)];
            [self.proView showViewblock:StringFromId(self.arrData[self.clickNum][@"templateId"]) Name:strName];
        }
    }
}
-(IBAction)talkBtnclick:(id)sender
{
    if(self.talkbtnflag){
        //交互
        [self.talkBtn setImage:[UIImage imageNamed:@"forscreen_talkclick.png"] forState:UIControlStateNormal];
        [self counstAceView];
        [self.view addSubview:self.aceDrawView];
        self.talkbtnflag = NO;
        NSString *hostid = self.arrData[self.clickNum][@"hostId"];
        NSMutableArray *hostIdarr = [NSMutableArray arrayWithArray:[hostid componentsSeparatedByString:@","]];
        TcpClient *tcp = [TcpClient sharedInstance];
        if(!tcp.asyncSocket.isConnected)
        {
            [[MBProgressController sharedInstance] showTipsOnlyText:@"TCP连接出错" AndDelay:1.5];
            return;
        }
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [tcp writeString:[Common hostidtostring:[hostIdarr[0] intValue] Metuod:@"GetEvents"]];
         });
    }
    else{
        //非交互
    [self.talkBtn setImage:[UIImage imageNamed:@"forscreen_talk.png"] forState:UIControlStateNormal];
        [self.aceDrawView removeFromSuperview];
        self.aceDrawView = nil;
        self.talkbtnflag = YES;
    }
}
//tap
-(void)tappro:(UIGestureRecognizer *)gesture
{
    [self.proView.view removeFromSuperview];
    [self.promeView removeFromSuperview];
    [self.messageBtn setImage:[UIImage imageNamed:@"forscreen_message.png"] forState:UIControlStateNormal];
}
#pragma -make proViewDelegate

-(void)promtblock
{
    [self.promeView removeFromSuperview];
    [self.messageBtn setImage:[UIImage imageNamed:@"forscreen_message.png"] forState:UIControlStateNormal];
}

@end
